package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupDto;
import com.lanjiu.lib.imclient.dto.SearchDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.dto.extra.SearchResultDto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.json.JsonUtils;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/20 21:10
 */
public class SearchListView extends IView {

    private SearchListCallback mCallback;

    public SearchListView(SearchListCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doFriendFind(String keyword) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_FIND_REGISTERED : EventConstants.Action.FRIEND_FIND_TOURISTS;
        SearchDto search = new SearchDto();
        search.setKeyword(keyword);
        doSendRequest(action, search.toJson());
    }

    public void doGroupFind(String keyword) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.GROUP_FIND_REGISTERED : EventConstants.Action.GROUP_FIND_TOURISTS;
        SearchDto search = new SearchDto();
        search.setKeyword(keyword);
        doSendRequest(action, search.toJson());
    }

    @Override
    public void onMessageEventHandle(MessageEvent event) {
        Logger.print("SearchResultView", event.toString());
        switch (event.action()) {
            case EventConstants.Action.FRIEND_FIND_TOURISTS:
            case EventConstants.Action.FRIEND_FIND_REGISTERED:
                SearchResultDto data = null;
                if (checkIsSuccess(event.code(), event.data()))
                    data = Dto.fromJson(event.data(), SearchResultDto.class);
                if (null != mCallback)
                    mCallback.onFriendFindCallback(event.code(), event.msg(), data);
                break;
            case EventConstants.Action.GROUP_FIND_TOURISTS:
            case EventConstants.Action.GROUP_FIND_REGISTERED:
                List<GroupDto> groupList = null;
                if (checkIsSuccess(event.code(), event.data()))
                    groupList = JsonUtils.convertJsonToList(event.data(), GroupDto.class);
                if (null != mCallback)
                    mCallback.onGroupFindCallback(event.code(), event.msg(), groupList);
                break;
        }
    }

    public interface SearchListCallback {

        void onFriendFindCallback(int code, String msg, SearchResultDto data);

        void onGroupFindCallback(int code, String msg, List<GroupDto> data);
    }
}
